package com.maker.task;

import com.maker.entity.OrderInfo;
import com.maker.service.AlipayDevelopService;
import com.maker.service.impl.OrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class AliPayTask {

    @Resource
    public OrderInfoService orderInfoService;
    @Resource
    private AlipayDevelopService alipayDevelopService;
    /**
     * 从第0秒开始每隔30秒执行1次，查询创建超过5分钟，并且未支付的订单
     */
    @Scheduled(cron= "0/30 * * * * ?")
    public void orderConfirm(){
        log.info("orderConfirm正在被执行......");

        List<OrderInfo> orderInfos = orderInfoService.getNoPayOrderByDuration(5);
        for(OrderInfo  orderInfo : orderInfos){
            String orderNo = orderInfo.getOrderNo();
            log.info("超时订单编====》{}",orderNo);
            //核实订单状态，调用支付宝查单接口
            alipayDevelopService.checkOrderStatus(orderNo);
        }
    }

    /**
     * 从第0秒开始每隔30秒执行1次，查询退款订单超过5分钟，并且已支付的订单
     */
    @Scheduled(cron= "0/50 * * * * ?")
    public void refundConfirm(){
        log.info("refundConfirm正在被执行......");

        List<OrderInfo> orderInfos = orderInfoService.getPayOrderByDuration(8);
        for(OrderInfo  orderInfo : orderInfos){
            String orderNo = orderInfo.getOrderNo();
            log.info("超时订单编====》{}",orderNo);
            //核实订单退款状态，调用支付宝查单接口
            alipayDevelopService.checkRefundOrderStatus(orderNo);
        }
    }
}
